package com.hxkcloud.patient.fragments;

import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.activitys.AddStopDrugActivity;
import com.hxkcloud.patient.adapter.StopDrugsAdapter;
import com.hxkcloud.patient.beans.StopDrugsRecordBean;
import com.hxkcloud.patient.beans.StopDrugsRecordWholeBean;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 停药记录
 */
public class StopDrugsRecordFragment extends BaseFragment implements PullToRefreshLayout.OnRefreshListener,AdapterView.OnItemClickListener{

    private View view;
    private LinearLayout emptyLayout;
    private TextView emptyTV;
    private PullToRefreshLayout refreshLayout;
    private PullableListView list_stop;

    private List<StopDrugsRecordBean> infoList;

    private StopDrugsAdapter adapter;

    private int pageIndex = 1;
    private int pageSize = 10;

    private int allSize = 0;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_STOPDRUGSRECORD_SUCCESS:
                StopDrugsRecordWholeBean wholeBean = (StopDrugsRecordWholeBean) msg.obj;
                allSize = Integer.valueOf(wholeBean.getTotalCount());

                if (pageIndex == 1)
                {
                    infoList.clear();
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    if (wholeBean.getList().size() > 0)
                    {
                        refreshLayout.setVisibility(View.VISIBLE);
                        emptyLayout.setVisibility(View.GONE);
                    } else
                    {
                        refreshLayout.setVisibility(View.GONE);
                        emptyLayout.setVisibility(View.VISIBLE);
                    }
                } else
                {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                infoList.addAll(wholeBean.getList());

                adapter.notifyDataSetChanged();

                break;
            case MessageCode.NETWORKCODE_STOPDRUGSRECORD_FAILURE:

                if (pageIndex == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                } else {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }

                if ((msg.obj != null) && (!TextUtils.isEmpty((String) msg.obj))) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_stop_drugs_record, container, false);
        initView();

        return view;
    }


    @Override
    protected void initView()
    {
        refreshLayout = (PullToRefreshLayout) view.findViewById(R.id.refreshlayout_takedrugs);
        list_stop = (PullableListView) view.findViewById(R.id.list_stop);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setCanPull(true, true);

        list_stop.setOnItemClickListener(this);


        emptyLayout = (LinearLayout) view.findViewById(R.id.ll_department_empty);
        emptyTV = (TextView) view.findViewById(R.id.empty_text);
        emptyTV.setText(getResources().getString(R.string.empty_text_mystopdrugs));

        infoList = new ArrayList<>();

        adapter = new StopDrugsAdapter(infoList,getActivity());
        list_stop.setAdapter(adapter);
    }

    @Override
    protected void initListener()
    {

    }

    @Override
    protected void initData()
    {
        loading();
        YiJiaHuNetWorkUtils.stopDrugsRecord(loginInfo.getPatientId(), pageIndex, pageSize, handler);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout)
    {
        loading();
        pageIndex = 1;
        YiJiaHuNetWorkUtils.stopDrugsRecord(loginInfo.getPatientId(), pageIndex, pageSize, handler);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout)
    {
        if(allSize == infoList.size())
        {
            toastMsg(R.string.nomore);
            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            return;
        }
        loading();
        pageIndex++;
        YiJiaHuNetWorkUtils.stopDrugsRecord(loginInfo.getPatientId(), pageIndex, pageSize, handler);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        AddStopDrugActivity.jumpToAddStopDrugActivity(getActivity(),null,1,infoList.get(position));
    }
}
